#pragma once

namespace shijie
{
	namespace utility
	{
		template <typename T>
		class AutoPtr
		{
		public:
			AutoPtr() : m_data(nullptr) {}
			AutoPtr(T *data) : m_data(data){}
			AutoPtr(AutoPtr &other) : m_data(other.release()) {
				// 从other获取所有权，并将other的指针置为nullptr
			}

			~AutoPtr() {
				if (m_data != nullptr) {
					delete m_data;
					m_data = nullptr;
				}
			} 

			T *get() {
				return m_data;
			}

			T *release() { // 返回一个指针，同时自己不再占用指针
				auto data = m_data;
				m_data = nullptr;
				return data;
			}

			void reset(T *data = nullptr) {
				if (m_data != data) {
					delete m_data;
					m_data = data;
				}
			}

			T * operator->() {
				return m_data;
			}

			T & operator * () {
				return *m_data;
			}
		
			AutoPtr & operator = (AutoPtr<T> &other) {
				if (this == &other) {
					return *this;
				}
				reset(other.release());
				return *this;
			}

		private : T * m_data;
		};
	}
}